<div class="tplay-body">
    <div class="layui-tab layui-tab-brief" lay-filter="docDemoTabBrief">
      <ul class="layui-tab-title">
        <li class="layui-this">评论</li>
        <li>留言</li>
      </ul>
      <div class="layui-tab-content">
        <div class="layui-tab-item layui-show tplay-btn">
          <button class="layui-btn layui-btn-primary layui-btn-sm" data-type="admin" data-events="shieldingMsg">禁止显示</button>
          <button class="layui-btn layui-btn-primary layui-btn-sm" data-type="admin" data-events="del">批量删除</button>
          <table id="tplay-admin-all" lay-filter="tplay-admin-all">
            <script type="text/html" id="admin_status">
              {{#  if(d.status == 1){ }}
                <span style="color:#1E9FFF">正常 </span>  
              {{#  } else { }}
                <span style="color:#FF5722">禁止显示 </span> 
              {{#  } }}
            </script>
          </table>
        </div>
        <div class="layui-tab-item tplay-btn">
          <button class="layui-btn layui-btn-primary layui-btn-sm" data-type="group" data-events="del_group">批量删除</button>
          <table id="tplay-admin-group" lay-filter="tplay-admin-group">
          </table>
        </div>
      </div>
    </div>
</div>

<script type="text/javascript">
layui.use(['table', 'element','tplay','apiconfig'], function(){
  var $ = layui.$
  ,table = layui.table
  ,element = layui.element
  ,tplay = layui.tplay
  ,config = layui.apiconfig;

  //评论
  table.render({
    elem: '#tplay-admin-all'
    ,url: config.domain+'/admin/user/message'
    ,where: {access_token: layui.data('tplay').access_token}
    ,page: true
    ,limit:15
    ,cols: [[
      {type: 'checkbox', fixed: 'left'}
      ,{field: 'id', title: 'ID'}
      ,{field: 'contentName', title: '文章/商品'}
      ,{field: 'user_id', title: '评论人ID'}
      ,{field: 'to_user_id', title: '回复对象ID'}
      ,{field: 'message', title: '评论内容'}
      ,{field: 'create_time', title: '评论时间'}
      ,{field: 'ip', title: '评论ip'}
      ,{field: 'status', title: '状态',toolbar: '#admin_status'}
    ]]
    ,skin: 'line'
  });
  
  //留言
  table.render({
    elem: '#tplay-admin-group'
    ,url: config.domain+'/admin/user/contact'
    ,page: true
    ,limit:15
    ,cols: [[
      {type: 'checkbox', fixed: 'left'}
      ,{field: 'id', title: 'ID'}
      ,{field: 'name', title: '姓名'}
      ,{field: 'phone', title: '电话'}
      ,{field: 'email', title: '邮箱'}
      ,{field: 'contact', title: '内容'}
      ,{field: 'create_time', title: '留言时间'}
    ]]
    ,skin: 'line'
  });

  //事件处理
  var events = {
    del: function(othis, type){
      var thisTabs = tabs[type]
      ,checkStatus = table.checkStatus(thisTabs.id)
      ,data = checkStatus.data; //获得选中的数据
      if(data.length === 0) return layer.msg('未选中行');
      var array = [];
      for (var i = 0; i < data.length; i++) {
         array.push(data[i].id);
      }
      layer.confirm('确定删除选中的数据吗？', function(){
        tplay.ajax({
          url:config.domain+"/admin/user/delMsg",
          type:'post',
          data:{id:array},
          success:function(res){
            layer.msg(res.msg);
            if(res.code == 1){
              table.reload(thisTabs.id); //刷新表格
            }
          }
        })
        
      });
    }
    ,shieldingMsg: function(othis, type){
      var thisTabs = tabs[type]
      ,checkStatus = table.checkStatus(thisTabs.id)
      ,data = checkStatus.data; //获得选中的数据
      if(data.length === 0) return layer.msg('未选中行');
      var array = [];
      for (var i = 0; i < data.length; i++) {
         array.push(data[i].id);
      }
      layer.confirm('确定禁用选中的数据吗？', function(){
        tplay.ajax({
          url:config.domain+"/admin/user/shieldingMsg",
          type:'post',
          data:{id:array},
          success:function(res){
            layer.msg(res.msg);
            if(res.code == 1){
              table.reload(thisTabs.id); //刷新表格
            }
          }
        })
        
      });
    }
    ,del_group: function(othis, type){
      var thisTabs = tabs[type]
      ,checkStatus = table.checkStatus(thisTabs.id)
      ,data = checkStatus.data; //获得选中的数据
      if(data.length === 0) return layer.msg('未选中行');
      var array = [];
      for (var i = 0; i < data.length; i++) {
         array.push(data[i].id);
      }
      layer.confirm('确定删除选中的数据吗？', function(){
        $.ajax({
          url:config.domain+"/admin/user/delContact",
          type:'post',
          data:{id:array},
          success:function(res){
            layer.msg(res.msg);
            if(res.code == 1){
              table.reload(thisTabs.id); //刷新表格
            }
          }
        })
        
      });
    }
  };

  //区分各选项卡中的表格
  var tabs = {
    admin: {
      text: '评论'
      ,id: 'tplay-admin-all'
    },
    group: {
      text: '留言'
      ,id: 'tplay-admin-group'
    }
  };
  
  $('.tplay-btn .layui-btn').on('click', function(){
    var othis = $(this)
    ,thisEvent = othis.data('events')
    ,type = othis.data('type');
    events[thisEvent] && events[thisEvent].call(this, othis, type);
  });
});
</script>
